<?php
/**
 * @author wyl
 * @date 2015年7月28日16:09:29
 */

Yii::import('application.components.widgets.CMenuProvider');
class AdminMenuProvider extends CMenuProvider{
	public $menuNav = array();
	public $position = 'nav';
	
	public function init(){
		$this->getConfig();
		return $this;
	}

	public function getConfig(){
		$filePath = Yii::getPathOfAlias('application.config.menu').'.php';
		if(file_exists($filePath)){
			$this->_configs=require $filePath;
		}else{
			throw new CException(Yii::t('yii','"{filePath}" is not defined.',array('{filePath}'=>$filePath)));
		}	
	}	
	
	public function active($active){
		if (array_key_exists($active, $this->_configs)) {
			$this->_configs[$active]['active']=true;
		}
		
	}
	
	public function selectSubMenu($masterName){
		$this->getConfig();
		$this->_configs = $this->_configs[$masterName]['items'];
		$this->position = 'left';
		return $this;
	}
	
	public function buildMenuData(){
		foreach ($this->_configs as $masterMenu => $masterMenuConfig){
			$items = array();
			if (array_key_exists('items', $masterMenuConfig)) {
				$items = $masterMenuConfig['items'];
				unset($masterMenuConfig['items']);
			}
			
			$this->_menu[$masterMenu] = $masterMenuConfig;
			
			if (!empty($items)) {
				foreach ($items as $subMenuKey => $subMenuConfig){
					if (in_array($this->position,$subMenuConfig['position'])) {
						$this->_menu[$masterMenu]['items'][$subMenuKey] = $subMenuConfig;
					}
				}
			}			
			
		}
	}
	/*
	public function addMenuItem($data){
		$this->menuNav[$data['label']] = $data;
	}
	
	public function addsubMenuItem($data){
		$this->menuSubItem[$data['label']] = $data;
	}
	
	public function buildMenuData(){
		$temp = array();
		$temp = array_merge($this->menuNav,$this->menuSubItem);
		return $temp; 
	}
	*/
	public function menuData(){
		$this->buildMenuData();
		return $this->_menu;
	}
}